package com.ywwxhz.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ywwxhz.model.AppInfo;
import com.ywwxhz.model.Category;
import com.ywwxhz.taskmgr.R;

import java.util.List;

import za.co.immedia.pinnedheaderlistview.SectionedBaseAdapter;

//自定义适配器类，提供给listView的自定义view
public class IgnoreListAdapter extends SectionedBaseAdapter {

    LayoutInflater infater = null;
    Context context;
    private List<Category> categories;

    public IgnoreListAdapter(Context context, List<Category> categories) {
        infater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.categories = categories;
        this.context = context;
    }

    public AppInfo getAppInfo(int pos) {
        return categories.get(getSectionForPosition(pos)).getAppInfo(
                getPositionInSectionForPosition(pos));
    }

    public void removeAppInfo(int pos) {
        categories.get(getSectionForPosition(pos)).remove(
                getPositionInSectionForPosition(pos));
    }

    @Override
    public Object getItem(int section, int position) {
        return categories.get(section).getAppInfo(position);
    }

    @Override
    public long getItemId(int section, int position) {
        return 0;
    }

    @Override
    public int getSectionCount() {
        return categories.size();
    }

    @Override
    public int getCountForSection(int section) {
        return categories.get(section).getSize();
    }

    @Override
    public View getItemView(int section, int position, View convertView,
                            ViewGroup parent) {
        View view = null;
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            view = infater.inflate(R.layout.ignore_list_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) convertView.getTag();
        }
        AppInfo appInfo = categories.get(section).getAppInfo(position);
        holder.appIcon.setImageDrawable(appInfo.getAppIcon());
        holder.tvAppLabel.setText(appInfo.getAppLabel());
        holder.tvPkgName.setText(appInfo.getPkgName());
        if (appInfo.isClickable()) {
            holder.tvcselect.setVisibility(View.VISIBLE);
            holder.tvcselect.setChecked(appInfo.isCheched());
        } else {
            holder.tvcselect.setVisibility(View.GONE);
            holder.tvcselect.setChecked(true);
        }
        return view;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView,
                                     ViewGroup parent) {
        LinearLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator
                    .inflate(R.layout.header_item, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        ((TextView) layout.findViewById(R.id.category_name)).setText(categories
                .get(section).getType());
        ((TextView) layout.findViewById(R.id.category_count))
                .setText(categories.get(section).getSize() + "");
        return layout;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    class ViewHolder {
        ImageView appIcon;
        TextView tvAppLabel;
        TextView tvPkgName;
        CheckBox tvcselect;

        public ViewHolder(View view) {
            this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
            this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
            this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
            this.tvcselect = (CheckBox) view.findViewById(R.id.checkBox1);
        }
    }
}